package com.scs.application.modules.adm.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scs.application.consts.GlobalConsts;
import com.scs.application.core.exception.BusinessException;
import com.scs.application.core.query.QueryContext;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.adm.entity.MatrApply;
import com.scs.application.modules.adm.entity.MatrApplyItem;
import com.scs.application.modules.adm.entity.MatrAudit;
import com.scs.application.modules.adm.mapper.MatrApplyItemMapper;
import com.scs.application.modules.adm.mapper.MatrApplyMapper;
import com.scs.application.modules.adm.mapper.MatrAuditMapper;
import com.scs.application.modules.adm.service.MatrApplyItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 耗材准入申请单明细 服务实现类
 * </p>
 *
 */
@Service
public class MatrApplyItemServiceImpl extends BaseServiceImpl<MatrApplyItemMapper, MatrApplyItem> implements MatrApplyItemService {
    @Autowired
    private MatrApplyMapper matrApplyMapper;

    @Autowired
    private MatrAuditMapper matrAuditMapper;

    @Override
    public boolean saveOrUpdate(MatrApplyItem entity) {
        //剔除空格
        entity.setMatrName(StringUtils.trim(entity.getMatrName()));
        entity.setMatrSpec(StringUtils.trim(entity.getMatrSpec()));
        entity.setMfgName(StringUtils.trim(entity.getMfgName()));

        if (StringUtils.isBlank(entity.getAuditId())){
            //该品名、品规、厂商的耗材是否已添加过
            Integer count = baseMapper.selectCount(Wrappers.<MatrApplyItem>query().eq("matr_name",entity.getMatrName())
                    .eq("matr_spec",entity.getMatrSpec())
                    .eq("mfg_name",entity.getMfgName())
                    .eq("apply_id",entity.getApplyId())
                    .ne(StringUtils.isNotBlank(entity.getId()),"id",entity.getId())
            );
            if (count != null && count > 0) {
                throw new BusinessException("该品名品规厂商的耗材已添加到申请明细，请勿重复提交");
            }

            //该品名、品规、厂商的耗材，是否存在已审批的审批单
            List<MatrApplyItem> matrApplyItems =baseMapper.selectList(Wrappers.<MatrApplyItem>query().eq("matr_name",entity.getMatrName())
                    .eq("matr_spec",entity.getMatrSpec())
                    .eq("mfg_name",entity.getMfgName())
                    .isNotNull("audit_id")
            );
            if (matrApplyItems != null && matrApplyItems.size() > 0) {
                List<String> auditIds = matrApplyItems.stream().map(MatrApplyItem::getAuditId).collect(Collectors.toList());
                List<MatrAudit> matrAudits =matrAuditMapper.selectList(Wrappers.<MatrAudit>query().in("id",auditIds)
                        .ne("flow_status", GlobalConsts.FLOW_END_VALUE)
                );
                if (matrAudits != null && matrAudits.size() > 0) {
                    MatrApply matrApply =matrApplyMapper.selectById(matrApplyItems.get(0).getApplyId());
                    MatrAudit matrAudit = matrAuditMapper.selectById(matrAudits.get(0).getId());
                    throw new BusinessException("该品名品规厂商的耗材正在进行准入审批，请勿重复提交<br>审批单号:%s<br>准入申请科室:%s",matrAudit.getBusKey(),matrApply.getDeptName());
                }
            }
        }

        return super.saveOrUpdate(entity);
    }
    @Override
    public Page<Map> pageForAuditAdd(QueryContext context, Map<String, Object> userParam) {
        return this.baseMapper.pageForAuditAdd(context.getPage(), context.getWrapper(), context.getParams(),userParam);
    }
}
